package com.dhl.rabbitmq.nine;

import com.dhl.rabbitmq.utils.RabbitConstant;
import com.dhl.rabbitmq.utils.RabbitUtils;
import com.rabbitmq.client.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import static com.dhl.rabbitmq.utils.RabbitConstant.X_MAX_PRIORITY;

/**
 * @author danghailiang
 * 优先级队列 消费者
 */
public class PriorityConsumer {
    public static void main(String[] args) throws Exception {
        // 获取信道
        Channel channel = RabbitUtils.getChannel();
        // 声明交换
        channel.exchangeDeclare("priorityExchange", BuiltinExchangeType.DIRECT,true,false,null);

        Map<String, Object> arguments = new HashMap<>(1);
        // 设置为优先级
        arguments.put(X_MAX_PRIORITY,250);
        // 声明队列
        channel.queueDeclare("priorityQueue",true,false,false,arguments);
        // 绑定
        channel.queueBind("priorityQueue","priorityExchange","priorityKey");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("接受的消息："+ new String(message.getBody(), StandardCharsets.UTF_8));
        };
        CancelCallback cancelCallback  = (consumerTag) -> {};
        /**
         * 1：队列名称
         * 2：是否自动应答
         * 3：成功回调
         * 4：失败回调
         */
        channel.basicConsume("priorityQueue",true,deliverCallback,cancelCallback);
    }
}
